package bb.lanxing.adapter;

import android.content.Context;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseListAdapter<T> extends BaseAdapter {
    protected List<T> dataSet;
    protected boolean loadMoreEnabled;
    protected boolean loadingMore;
    protected Context mContext;

    @Override
    public long getItemId(int position) {
        return position;
    }

    public BaseListAdapter(Context context, List<T> dataSet) {
        this.loadingMore = false;
        this.loadMoreEnabled = false;
        if (context == null) {
            throw new IllegalArgumentException("context can not be null !");
        }
        this.mContext = context;
        this.dataSet = dataSet;
        if (dataSet == null) {
            this.dataSet = new ArrayList();
        }
    }

    @Override
    public int getCount() {
        List<T> list = this.dataSet;
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public T getItem(int position) {
        List<T> list = this.dataSet;
        if (list == null) {
            return null;
        }
        return list.get(position);
    }

    public void update(List<T> data, boolean clear) {
        if (clear) {
            this.dataSet.clear();
        }
        this.dataSet.addAll(data);
        notifyDataSetChanged();
    }

    public void insert(T obj) {
        this.dataSet.add(obj);
        notifyDataSetChanged();
    }

    public void insert(int location, T obj) {
        this.dataSet.add(location, obj);
        notifyDataSetChanged();
    }

    public void insert(int location, List<T> list) {
        this.dataSet.addAll(location, list);
        notifyDataSetChanged();
    }

    public void insert(List<T> list) {
        this.dataSet.addAll(list);
        notifyDataSetChanged();
    }

    public T delete(int location) {
        T remove = this.dataSet.remove(location);
        notifyDataSetChanged();
        return remove;
    }

    public boolean delete(T obj) {
        boolean remove = this.dataSet.remove(obj);
        if (remove) {
            notifyDataSetChanged();
        }
        return remove;
    }

    public void deleteAll() {
        this.dataSet.clear();
        notifyDataSetChanged();
    }
}